package com.bxd.baodan.activity;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.bxd.baodan.R;
import com.bxd.baodan.app.JsonApi;
import com.bxd.baodan.item.InsuranceTypeBean;
import com.bxd.baodan.item.ManualAddInsuranceBean;
import com.bxd.baodan.util.CommomDialog;
import com.bxd.baodan.util.JSONUtil;
import com.bxd.baodan.util.NetUtil;
import com.bxd.baodan.widget.MyRadioGroup;

import java.util.List;

public class ManualAdd1Activity extends BaseActivity implements View.OnClickListener {

    private TextView mTvCancel;
    private MyRadioGroup mMyRadioGroup;
    private RelativeLayout mRlNext;
    private TextView mTvCompany;
    private EditText mEtInsuranceName;
    private String insuranceType;

    private String companyId;

    private List<InsuranceTypeBean.ListBean> mData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manual_add1);
        hideBar();
        initView();
        initInsuranceType();
    }

    private void initInsuranceType() {
        NetUtil netUtil = new NetUtil(this, JsonApi.INSURANCE_CATEGORY);
        netUtil.postRequest("正在加载数据...", new NetUtil.RequestStringListener() {
            @Override
            public void onComplete(String result) {
                InsuranceTypeBean mBean = new Gson().fromJson(JSONUtil.getData(result),
                        new TypeToken<InsuranceTypeBean>() {
                        }.getType());
                mData =  mBean.getList();
                showRadioButton(mData);
            }
        });
    }

    private void showRadioButton(List<InsuranceTypeBean.ListBean> data) {
        if (data == null || data.size() == 0) {
            return;
        }
        RadioButton button1 = (RadioButton) mMyRadioGroup.findViewById(R.id.radio_button_1);
        RadioButton button2 = (RadioButton) mMyRadioGroup.findViewById(R.id.radio_button_2);
        RadioButton button3 = (RadioButton) mMyRadioGroup.findViewById(R.id.radio_button_3);
        RadioButton button4 = (RadioButton) mMyRadioGroup.findViewById(R.id.radio_button_4);
        RadioButton button5 = (RadioButton) mMyRadioGroup.findViewById(R.id.radio_button_5);
        RadioButton button6 = (RadioButton) mMyRadioGroup.findViewById(R.id.radio_button_6);

        button1.setVisibility(View.VISIBLE);
        button2.setVisibility(View.VISIBLE);
        button3.setVisibility(View.VISIBLE);
        button4.setVisibility(View.VISIBLE);
        button5.setVisibility(View.VISIBLE);
        button6.setVisibility(View.VISIBLE);

        if (!TextUtils.isEmpty(data.get(0).getName())){button1.setText(data.get(0).getName());}
        if (!TextUtils.isEmpty(data.get(1).getName())){button2.setText(data.get(1).getName());}
        if (!TextUtils.isEmpty(data.get(2).getName())){button3.setText(data.get(2).getName());}
        if (!TextUtils.isEmpty(data.get(3).getName())){button4.setText(data.get(3).getName());}
        if (!TextUtils.isEmpty(data.get(4).getName())){button5.setText(data.get(4).getName());}
        if (!TextUtils.isEmpty(data.get(5).getName())){button6.setText(data.get(5).getName());}

        if (data.size() < 6) {
            mMyRadioGroup.findViewById(R.id.radio_button_6).setVisibility(View.INVISIBLE);
        }
        if (data.size() < 5) {
            mMyRadioGroup.findViewById(R.id.radio_button_5).setVisibility(View.INVISIBLE);
        }
        if (data.size() < 4) {
            mMyRadioGroup.findViewById(R.id.radio_button_4).setVisibility(View.INVISIBLE);
        }
        if (data.size() < 3) {
            mMyRadioGroup.findViewById(R.id.radio_button_3).setVisibility(View.INVISIBLE);
        }
        if (data.size() < 2) {
            mMyRadioGroup.findViewById(R.id.radio_button_2).setVisibility(View.INVISIBLE);
        }
    }

    private void initView() {
        mTvCancel = getTextView(R.id.tv_cancel);
        mTvCancel.setOnClickListener(this);
        mMyRadioGroup = (MyRadioGroup) findViewById(R.id.radio_group);
        mRlNext = getRelativeLayout(R.id.rl_next);
        mRlNext.setOnClickListener(this);
        mTvCompany = getTextView(R.id.tv_company);
        mTvCompany.setOnClickListener(this);
        mEtInsuranceName = getEditText(R.id.et_insurance_name);

        mMyRadioGroup.setOnCheckedChangeListener(new MyRadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(MyRadioGroup group, int checkedId) {
                if(mData ==  null || mData.size()==0){
                    return;
                }
                switch (checkedId) {
                    case R.id.radio_button_1:
                        insuranceType = mData.get(0).getId();
                        break;
                    case R.id.radio_button_2:
                        insuranceType = mData.get(1).getId();
                        break;
                    case R.id.radio_button_3:
                        insuranceType = mData.get(2).getId();
                        break;
                    case R.id.radio_button_4:
                        insuranceType = mData.get(3).getId();
                        break;
                    case R.id.radio_button_5:
                        insuranceType = mData.get(4).getId();
                        break;
                    case R.id.radio_button_6:
                        insuranceType = mData.get(5).getId();
                        break;
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_cancel:
                back();

                break;
            case R.id.tv_company:
                // 选择公司
                Drawable drawable = getResources().getDrawable(R.drawable.p_check_company);
                drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                mTvCompany.setCompoundDrawables(null, null, drawable, null);
                mTvCompany.setTextColor(Color.parseColor("#37b059"));
                openActivityForResult(AllCompanyActivity.class, new String[]{"company_name"}, new String[]{mTvCompany.getText().toString()});
                break;
            case R.id.rl_next:
                // 下一步
                if (TextUtils.isEmpty(insuranceType)) {
                    showToast("请选择保险类型！");
                    return;
                }
                String company = getString(mTvCompany);
                if (TextUtils.isEmpty(company) || company.equals("请选择保险公司")) {
                    showToast("请选择保险公司！");
                    return;
                }
                String insuranceName = getString(mEtInsuranceName);
                if (TextUtils.isEmpty(insuranceName)) {
                    showToast("请输入保险名称！");
                    return;
                }

                ManualAddInsuranceBean bean = new ManualAddInsuranceBean();
                bean.catId = insuranceType;
                bean.companyId = companyId;
                bean.productName = insuranceName;

                Bundle b = new Bundle();
                b.putSerializable("bean", bean);
                openActivity(ManualAdd2Activity.class, b);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Drawable drawable = getResources().getDrawable(R.drawable.p_uncheck_company);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        mTvCompany.setCompoundDrawables(null, null, drawable, null);
        mTvCompany.setTextColor(getResources().getColor(R.color.text_black));
        if (resultCode == 200) {
            mTvCompany.setText(data.getStringExtra("company_name"));
            companyId = data.getStringExtra("company_id");
            if (companyId == null) {
                companyId = "0";
            }
        }
    }

    /**
     * 退出
     */
    private void back() {
        new CommomDialog(ManualAdd1Activity.this, R.style.insurance_dialog, "确认退出添加保单？", new CommomDialog.OnCloseListener() {
            @Override
            public void onClick(Dialog dialog, boolean confirm) {
                if (confirm) {
                    dialog.dismiss();
                    ManualAdd1Activity.this.finish();
                }
            }
        }).setTitle("退出添加").show();
    }

    @Override
    public void onBackPressed() {
        back();
    }
}
